<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from 'axios';
import { mapState } from 'vuex'
import { getProvinceMapInfo } from '../utils/map_utils';
export default {
  data() {
    return {
      allData: [],
      chartInstance: null,
      provinceCacheData: {},
    };
  },

  created() {
    window.addEventListener('resize', this.screenAdapter);
    // 注册回调函数
    this.$socket.registerCallBack('mapData', this.getData);
  },

  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: '',
    });
    this.screenAdapter();
  },

  beforeDestroy() {
    window.removeEventListener('resize', this.screenAdapter);

    this.$socket.unRegisterCallBack('mapData');
  },

  methods: {
    // init chart
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs['map_ref'], this.theme);
      const ret = await axios.get(
        'http://localhost:8999/static/map/china.json'
      );
      
      this.$echarts.registerMap('china', ret.data);

      // handle fixed options
      const initOption = {
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            // map color
            areaColor: '#2e72bf',
            borderColor: '#333',
          },
        },
        title: {
          text: '‖ 商家分布与销量排行',
          left: 20,
          top: 20,
        },
        legend: {
          left: '5%',
          bottom: '5%',
          // 图例摆放的方向
          orient: 'vertical',
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on('click', async (arg) => {
        const retProvince = getProvinceMapInfo(arg.name);
        if (!this.provinceCacheData[retProvince.key]) {
          const province = await axios.get(
            `http://localhost:8999${retProvince.path}`
          );
          this.provinceCacheData[retProvince.key] = province.data;
          this.$echarts.registerMap(retProvince.key, province.data);
        }
        // 地图注册一次就行
        // this.$echarts.registerMap(
        //   retProvince.key,
        //   this.provinceCacheData[retProvince.key]
        // );

        const option = {
          geo: {
            map: retProvince.key,
          },
        };
        this.chartInstance.setOption(option);
      });

      this.chartInstance.on('dblclick', (arg) => {
        console.log(arg);
      });
    },

    // async getData() {
    //   const { data } = await this.$http.get('map');
    //   this.allData = data;
    //   this.updateChart();
    // },
    getData(data) {
      this.allData = data;
      this.updateChart();
    },

    updateChart() {
      const seriesArr = this.allData.map((item) => {
        // if need to show data on map, should config add configuration to chart.  coordinateSystem: 'geo'
        return {
          type: 'effectScatter',
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo',
          rippleEffect: {
            scale: 10, // 涟漪动画大小
            brushType: 'stroke',
          },
        };
      });
      const legendArr = this.allData.map((item) => item.name);
      // handle data options
      const dataOption = {
        series: seriesArr,
        legend: {
          data: legendArr,
        },
      };
      this.chartInstance.setOption(dataOption);
    },

    screenAdapter() {
      const titleFontsize = (this.$refs['map_ref'].offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontsize,
          },
        },
        legend: {
          itemWidth: titleFontsize / 2,
          itemHeight: titleFontsize / 2,
          // 图例的间隔
          itemGap: titleFontsize / 2,
          textStyle: {
            fontSize: titleFontsize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },

    revertMap() {
      const option = {
        geo: {
          map: 'china',
        },
      };
      this.chartInstance.setOption(option);
    },
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
};
</script>

<style scoped lang="scss"></style>
